package vno.fux.res.pool
{
	import flash.utils.Dictionary;

	public class ResourcePool
	{
		private static var instance:ResourcePool;
		private var _mapRes:ResourceList;
		private var _chaRes:ResourceList;
		private var _uiRes:ResourceList;
		private var _sndRes:ResourceList;
		private var _settings:XML;
		private var _currentInventory:ResourceInventory;

		public function ResourcePool(pvt:PrivateClass, info_:XML)
		{
			_settings = info_;
			initMapResourece();
			initCharacterResource();
			initUiResource();
			initSoundResource();
		}
		
		public static function init(info_:XML):void
		{
			if(ResourcePool.instance)
			{
				throw new Error("You can't init the ResourcePool twice!")
			}
			ResourcePool.instance = new ResourcePool(new PrivateClass, info_);
		}

		public function setCurrentInventory(id_:uint):void
		{
			var ivtSet:ResourceInventory = ResourceInventory.getRI(id_);
			var addSet:ResourceInventory;
			var delSet:ResourceInventory;
			if(_currentInventory)			
			{
				addSet = ivtSet.intersect(_currentInventory);
				delSet = _currentInventory.intersect(ivtSet);
				_mapRes.clearByHashSet(delSet.map);
				_chaRes.clearByHashSet(delSet.character);
				_uiRes.clearByHashSet(delSet.ui);
				_sndRes.createByHashSet(delSet.snd);
			}
			else
			{
				addSet = ivtSet;
			}
			_mapRes.createByHashSet(addSet.map);
			_chaRes.createByHashSet(addSet.character);
			_uiRes.createByHashSet(addSet.ui);
			_sndRes.createByHashSet(addSet.snd);

			_currentInventory = ivtSet;
		}

		private function initCharacterResource():void
		{
			_chaRes = new ResourceList;
			var playerSetting:XML = createPlayerSetting();
			for each(var pl:XML in playerSetting.assets)
			{
				var pr:PlayerResource = new PlayerResource(pl.@id, pl);
				_chaRes.setRes(uint(pl.@id), pr) ;
			}
			var npcSetting:XML = createNpcSetting();
			for each(var npc:XML in npcSetting.assets)
			{
				var nr:NpcResource = new NpcResource(npc.@id, npc);
				_chaRes.setRes(uint(npc.@id), nr);
			}
		}
		
		private function createPlayerSetting():XML//written by ky
		{
			var chaRes:XML=<chaRes/>;
			for(var i:int =0;_settings.characterRes[i]!=null;i++)
			{
				chaRes.appendChild(<player/>);
				chaRes.player=_settings.characterRes.playerRes.assets;
			}

			return chaRes;
		}
		
		private function createNpcSetting():XML//written by ky
		{
			var chaRes:XML=<chaRes/>;
			for(var i:int =0;_settings.characterRes[i]!=null;i++)
			{
				chaRes.appendChild(<npc/>);
				chaRes.npc=_settings.characterRes.npcRes.assets;
			}
			return chaRes;
		}

		private function initMapResourece():void
		{
			_mapRes = new ResourceList;
			var mapSetting:XML = createMapSetting();
			for each(var map:XML in mapSetting.map)
			{
				var mr:MapResource = new MapResource(map.@id, map);
				_mapRes.setRes(uint(map.@id), mr);
			}
		}
		
		private function createMapSetting():XML//written by ky
		{
			 var mapRes:XML=<mapRes/>;
			 for(var i:int =0;_settings.mapRes.map[i]!=null;i++)
			 {
			 	mapRes.appendChild(<map/>);
			 	mapRes.map[i].@id=_settings.mapRes.map[i].@id;
			 	mapRes.map[i].appendChild(<tiles/>);
			 	mapRes.map[i].tiles.@numX=_settings.mapRes.map[i].tiles.@numX;
			 	mapRes.map[i].tiles.@numY=_settings.mapRes.map[i].tiles.@numY;
			 }
			 return mapRes;
		}
		
		private function initUiResource():void
		{
			_uiRes = new ResourceList;
			var uiSetting:XML = createUiSetting();
			_uiRes.setRes(uint(uiSetting.walkTarget.assets.@id), 
				new WalkTargetResource(uiSetting.walkTarget.assets.@id));
			_uiRes.setRes(uint(uiSetting.bgmButton.assets.@id), 
				new PicButtonResource(uiSetting.bgmButton.assets.@id));
			_uiRes.setRes(uint(uiSetting.minimapBorder.assets.@id), 
				new MinimapResource(uiSetting.minimapBorder.assets.@id));
		}
		private function createUiSetting():XML
		{
			var result:XML = _settings.uiRes[0];
			return result;
		}
		private function initSoundResource():void
		{
			_sndRes = new ResourceList;
			var sndSetting:XML = createSoundSetting();
			_sndRes.setRes(uint(sndSetting.mainBgm.assets.@id), 
				new SoundResource(sndSetting.mainBgm.assets.@id));
		}
		
		private function createSoundSetting():XML
		{
			var result:XML =_settings.sndRes[0];
			return result;
		}
		public static function getInstance():ResourcePool
		{
			return ResourcePool.instance;
		}
		
		public function get mapRes():ResourceList
		{
			return _mapRes;
		}
		
		public function get settings():XML
		{
			return _settings;
		}

		public function set settings(value:XML):void
		{
			_settings = value;
		}

		public function get chaRes():ResourceList
		{
			return _chaRes;
		}
		public function get uiRes():ResourceList
		{
			return _uiRes;
		}
		public function get sndRes():ResourceList
		{
			return _sndRes;
		}
	}
}
class PrivateClass
{
	public function PrivateClass()
	{
	}
}